package com.lalilu.homework7.fragment;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.lalilu.homework7.R;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class Fragment_2 extends Fragment {

    private static final String EXTERNAL_FILE_NAME = "data.txt";

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        TextView title = view.findViewById(R.id.Title);
        TextView insideText = view.findViewById(R.id.insideText);
        EditText inputer = view.findViewById(R.id.textInput);
        Button insert = view.findViewById(R.id.insert);
        Button read = view.findViewById(R.id.read);

        title.setText("外部存储示例");
        insideText.setText(getString(EXTERNAL_FILE_NAME, requireActivity()));

        insert.setOnClickListener(v -> {
            setString(EXTERNAL_FILE_NAME, requireActivity(), inputer.getText().toString());
            inputer.setText("");
            insideText.setText(getString(EXTERNAL_FILE_NAME, requireActivity()));
        });

        read.setOnClickListener(v -> {
            String temp = getString(EXTERNAL_FILE_NAME, requireActivity());
            inputer.setText(temp);
            insideText.setText(temp);
        });
    }

    public static void setString(String FileName, Activity activity, String text) {
        try {
            File externalFilesDir = activity.getExternalFilesDir(null);
            File externalFile = new File(externalFilesDir, FileName);

            FileOutputStream fileOutputStream = new FileOutputStream(externalFile);
            BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(fileOutputStream));
            bufferedWriter.write(text);
            bufferedWriter.flush();
            bufferedWriter.close();
            fileOutputStream.close();
        } catch (Exception e) {
            Toast.makeText(activity, e.getClass().getName() + ": " + e.getMessage(), Toast.LENGTH_SHORT).show();
            e.printStackTrace();
        }
    }

    public static String getString(String FileName, Activity activity) {
        String temp = "null";
        try {
            File externalFilesDir = activity.getExternalFilesDir(null);
            File externalFile = new File(externalFilesDir, FileName);

            FileInputStream fileInputStream = new FileInputStream(externalFile);
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(fileInputStream));
            temp = bufferedReader.readLine();
            bufferedReader.close();
            fileInputStream.close();
        } catch (Exception e) {
            Toast.makeText(activity, e.getClass().getName() + ": " + e.getMessage(), Toast.LENGTH_SHORT).show();
            e.printStackTrace();
        }
        return temp;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_1, container, false);
    }
}